package com.bwf.lock;

/**
 * @Version v1.0
 * @Description: TODO
 * @Author ZHANZHONGXIN
 * @Date 2022/5/20 13:37
 * @Copyright 博为峰
 */
public class Consumer implements Runnable {
    private Resource reource;

    public Consumer(Resource reource) {
        this.reource = reource;
    }

    @Override
    public void run() {
        for (int i = 0; i < 5; i++) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (reource.object) {
                if (!reource.flag) {
                    try {
                        reource.object.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                reource.count--;
                System.out.println(Thread.currentThread().getName() + ",消费者:" + reource.count);
                reource.flag = false;
                reource.object.notify();
            }
        }
    }
}
